Serialize And Deserialize Python Variables Using MessagePack

Overview:

  • MessagePack is a serialization format and also a serialization framework.
  • MessagePack is like JSON, when it comes to how the data is specified. The data is tagged along with its type. There is no separate definition of schema involved, while specifying data using MessagePack as in the case of Protocol Buffers.
  • Unlike JSON, MessagePack is a binary format.
  • Being a binary format, the data stored in MessagePack format is more compact than JSON.
  • Writing and reading of data using MessagePack is much faster than JSON due to the binary format of MessagePack data.
  • This article explains serializing and deserializing Python primitive types individually as well as using streams.

 

Installing MessagePack:

MessagePack for Python can be installed using the following command

pip install msgpack

 

Serializing individual variables using MessagePack:

  • The Python primitive types are serialized using the method packb()which packs the values as bytes.
  • The Python primitive types are deserialized using the method unpackb()which converts the bytes back into values of primitive types.

 

Example:

import msgpack

 

# Serialize and deserialize string data

toSerialize = "Hello World"

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("String data serialized: {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("String data deserialized: {}".format(deserialized))

 

# Serialize and deserialize integer data

toSerialize = 76655443

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("Integer data serialized: {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("Integer data deserialized: {}".format(deserialized))

 

# Serialize and deserialize double data

toSerialize = 98.76

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("Double data serialized: {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("Double data deserialized: {}".format(deserialized))

 

# Serialize and deserialize Boolean data for the value True

toSerialize = False

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("Boolean data serialized (True): {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("Boolean data deserialized (True): {}".format(deserialized))

 

# Serialize and deserialize Boolean data for the value False

toSerialize = True

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("Boolean data serialized (False): {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("Boolean data deserialized (False): {}".format(deserialized))

 

# Serialize and deserialize None

toSerialize = None

serialized  = msgpack.packb(toSerialize, use_bin_type=True)

print("None value serialized: {}".format(serialized))

deserialized  = msgpack.unpackb(serialized, raw=False)

print("None value deserialized: {}".format(deserialized))

 

Output:

String data serialized: b'\xabHello World'

String data deserialized: Hello World

Integer data serialized: b'\xce\x04\x91\xabS'

Integer data deserialized: 76655443

Double data serialized: b'\xcb@X\xb0\xa3\xd7\n=q'

Double data deserialized: 98.76

Boolean data serialized (True): b'\xc2'

Boolean data deserialized (True): False

Boolean data serialized (False): b'\xc3'

Boolean data deserialized (False): True

None value serialized: b'\xc0'

None value deserialized: None


Copyright 2023 © pythontic.com